package org.abratuhi.kvngroupware.server;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Properties;

import org.abratuhi.kvngroupware.model.Brainstorm;
import org.abratuhi.kvngroupware.model.Chatroom;
import org.abratuhi.kvngroupware.model.User;
import org.abratuhi.kvngroupware.network.Message;
import org.abratuhi.kvngroupware.network.Server;
import org.jdom.Element;

public class KVNGroupwareServer {
	
	public final static String serverFile = "server.properties";
	
	Properties serverprops;
	
	ArrayList<Chatroom> chatrooms = new ArrayList<Chatroom>();
	ArrayList<Brainstorm> brainstorms = new ArrayList<Brainstorm>();
	ArrayList<User> users = new ArrayList<User>();
	
	Server netserver;
	ServerMPTUA mptua;
	ServerDB db;
	
	public KVNGroupwareServer(){
		this.serverprops = loadProperties(serverFile);
		this.netserver = new Server(this, Integer.valueOf(serverprops.getProperty("server.port")));
		this.mptua = new ServerMPTUA(this);
		this.db = new ServerDB(this, serverprops);
	}
	
	public ServerMPTUA getMPTUA(){
		return this.mptua;
	}
	public ServerDB getDB(){
		return this.db;
	}
	
	public Brainstorm getBrainstorm(String id){
		for(int i=0; i<brainstorms.size(); i++){
			if(brainstorms.get(i).getId().equals(id)){
				return brainstorms.get(i);
			}
		}
		return null;
	}
	public User getUser(String id){
		for(int i=0; i<users.size(); i++){
			if(users.get(i).getId().equals(id)){
				return users.get(i);
			}
		}
		return null;
	}
	public Chatroom getChatroom(String id){
		for(int i=0; i<chatrooms.size(); i++){
			if(chatrooms.get(i).getId().equals(id)){
				return chatrooms.get(i);
			}
		}
		return null;
	}
	
	public void removeUser(String id){
		for(int i=0; i<brainstorms.size(); i++){
			brainstorms.get(i).removeMember(id);
		}
		for(int i=0; i<chatrooms.size(); i++){
			chatrooms.get(i).removeMember(id);
		}
		for(int i=0; i<users.size(); i++){
			if(users.get(i).getId().equals(id)){
				users.remove(i);
			}
		}
	}
	
	public void removeBrainstorm(String id){
		for(int i=0; i<brainstorms.size(); i++){
			if(brainstorms.get(i).getId().equals(id)){
				brainstorms.remove(i);
			}
		}
	}
	
	public void removeChatroom(String id){
		for(int i=0; i<chatrooms.size(); i++){
			if(chatrooms.get(i).getId().equals(id)){
				chatrooms.remove(i);
			}
		}
	}
	
	public void start(){
		this.netserver.up();
		this.netserver.start();
	}
	
	public void stop(){
		this.netserver.stop();
		this.netserver.down();
	}
	
	public void clear(){
		this.brainstorms.clear();
		this.chatrooms.clear();
		this.users.clear();
	}
	
	public void exit(){
		System.exit(0);
	}
	
	/**/
	public String getSummary(){
		StringWriter sw = new StringWriter();
		sw.append("**************************************************"+"\n");
		sw.append("*************** GROUPWARE STATISTICS: ************"+"\n");
		sw.append("**************************************************"+"\n");
		
		sw.append("Users:\t");
		for(int i=0; i<users.size();i++){
			sw.append(users.get(i).getId()+" ");
		}
		sw.append("\n");
		 
		sw.append("Brainstorms:\t");
		for(int i=0; i<brainstorms.size();i++){
			sw.append(brainstorms.get(i).getSummary()+" ");
		}
		sw.append("\n");
		
		sw.append("Chatrooms:\t");
		for(int i=0; i<chatrooms.size();i++){
			sw.append(chatrooms.get(i).getSummary()+" ");
		}
		sw.append("\n");
		
		return sw.toString();
	}
	
	/**/

	public void proceedMessage(Message message) {
		System.out.println("Server:\tproceeding message:\t"+message.toString());
		this.mptua.proceedMessage(message);
	}
	
	public Message createMessage(String[] attrNames, String[] attrValues, String text, Element[] content){
		return mptua.createMessage(attrNames, attrValues, text, content);
	}
	
	public void sendMessage(Message message){
		for(int i=0; i<netserver.clients.size(); i++){
			netserver.clients.get(i).sendMessage(message);
		}
	}
	
	public Properties loadProperties(String file){
		Properties props = new Properties();
		try {
			if(file.endsWith(".xml")){
		        props.loadFromXML(new FileInputStream(file));
			}
			else if(file.endsWith(".properties") || file.endsWith(".preferences")){
				props.load(new FileInputStream(file));
			}
	    } catch (IOException e) {
	    	e.printStackTrace();
	    }
	    return props;
	}
	
	public static void main(String[] args){
		KVNGroupwareServer kvnserver = new KVNGroupwareServer();
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		
		while(true){
			System.out.print("cmdsh > ");
			try {
				String command = in.readLine();
				if(command.equals("quit")){
					kvnserver.exit();
					break;
				}
				else if(command.equals("start")){
					kvnserver.start();
				}
				else if(command.equals("stop")){
					kvnserver.stop();
				}
				else if(command.equals("info")){
					System.out.println(kvnserver.netserver.getSummary());
					System.out.println(kvnserver.getSummary());
				}
				else if(command.equals("clear")){
					kvnserver.clear();
				}
				else if(command.equals("help")){
					System.out.println("Recognized commandos:\nstart\nstop\ninfo\nhelp\nquit");
				}
				else{
					System.out.println("'"+command+"': Unrecognized command, print 'help' to get the list available commandos.");
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}
